

package com.liulishuo.filedownloader.i;


import com.liulishuo.filedownloader.message.MessageSnapshot;
import com.liulishuo.filedownloader.model.FileDownloadHeader;
import com.liulishuo.filedownloader.model.FileDownloadTaskAtom;
import ohos.event.notification.NotificationRequest;
import com.liulishuo.filedownloader.i.IFileDownloadIPCCallback;
import com.liulishuo.filedownloader.i.FileDownloadIPCCallbackStub;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;
import ohos.rpc.MessageParcel;
import ohos.rpc.MessageOption;

public class FileDownloadIPCServiceProxy implements IFileDownloadIPCService {
    public final String TAG = FileDownloadIPCServiceProxy.class.getSimpleName();
    private static final String DESCRIPTOR = "com.liulishuo.filedownloader.i.IFileDownloadIPCService";

    private static final int COMMAND_REGISTER_CALLBACK = IRemoteObject.MIN_TRANSACTION_ID + 0;
    private static final int COMMAND_UNREGISTER_CALLBACK = IRemoteObject.MIN_TRANSACTION_ID + 1;
    private static final int COMMAND_CHECK_DOWNLOADING = IRemoteObject.MIN_TRANSACTION_ID + 2;
    private static final int COMMAND_START = IRemoteObject.MIN_TRANSACTION_ID + 3;
    private static final int COMMAND_PAUSE = IRemoteObject.MIN_TRANSACTION_ID + 4;
    private static final int COMMAND_PAUSE_ALL_TASKS = IRemoteObject.MIN_TRANSACTION_ID + 5;
    private static final int COMMAND_SET_MAX_NETWORK_THREAD_COUNT = IRemoteObject.MIN_TRANSACTION_ID + 6;
    private static final int COMMAND_GET_SOFAR = IRemoteObject.MIN_TRANSACTION_ID + 7;
    private static final int COMMAND_GET_TOTAL = IRemoteObject.MIN_TRANSACTION_ID + 8;
    private static final int COMMAND_GET_STATUS = IRemoteObject.MIN_TRANSACTION_ID + 9;
    private static final int COMMAND_IS_IDLE = IRemoteObject.MIN_TRANSACTION_ID + 10;
    private static final int COMMAND_START_FOREGROUND = IRemoteObject.MIN_TRANSACTION_ID + 11;
    private static final int COMMAND_STOP_FOREGROUND = IRemoteObject.MIN_TRANSACTION_ID + 12;
    private static final int COMMAND_CLEAR_TASK_DATA = IRemoteObject.MIN_TRANSACTION_ID + 13;
    private static final int COMMAND_CLEAR_ALL_TASK_DATA = IRemoteObject.MIN_TRANSACTION_ID + 14;

    private final IRemoteObject remote;
    private static final int ERR_OK = 0;

    public FileDownloadIPCServiceProxy(
            /* [in] */ IRemoteObject remote) {
        this.remote = remote;
    }

    @Override
    public IRemoteObject asObject() {
        return remote;
    }

    @Override
    public void registerCallback(
            /* [in] */ IFileDownloadIPCCallback callback) throws RemoteException {


        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeRemoteObject(callback.asObject());

        try {
            remote.sendRequest(COMMAND_REGISTER_CALLBACK, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void unregisterCallback(
            /* [in] */ IFileDownloadIPCCallback callback) throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeRemoteObject(callback.asObject());

        try {
            remote.sendRequest(COMMAND_UNREGISTER_CALLBACK, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public boolean checkDownloading(
            /* [in] */ String url,
            /* [in] */ String path) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeString(url);
        data.writeString(path);

        try {
            remote.sendRequest(COMMAND_CHECK_DOWNLOADING, data, reply, option);
            reply.readException();
            boolean result = reply.readInt() == 1 ? true : false;
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void start(
            /* [in] */ String url,
            /* [in] */ String path,
            /* [in] */ boolean pathAsDirectory,
            /* [in] */ int callbackProgressTimes,
            /* [in] */ int callbackProgressMinIntervalMillis,
            /* [in] */ int autoRetryTimes,
            /* [in] */ boolean forceReDownload,
            /* [in] */ FileDownloadHeader header,
            /* [in] */ boolean isWifiRequired) throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeString(url);
        data.writeString(path);
        data.writeInt(pathAsDirectory ? 1 : 0);
        data.writeInt(callbackProgressTimes);
        data.writeInt(callbackProgressMinIntervalMillis);
        data.writeInt(autoRetryTimes);
        data.writeInt(forceReDownload ? 1 : 0);
        data.writeSequenceable(header);
        data.writeInt(isWifiRequired ? 1 : 0);

        try {
            remote.sendRequest(COMMAND_START, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public boolean pause(
            /* [in] */ int downloadId) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(downloadId);

        try {
            remote.sendRequest(COMMAND_PAUSE, data, reply, option);
            reply.readException();
            boolean result = reply.readInt() == 1 ? true : false;
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void pauseAllTasks() throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        try {
            remote.sendRequest(COMMAND_PAUSE_ALL_TASKS, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public boolean setMaxNetworkThreadCount(
            /* [in] */ int count) throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(count);

        try {
            remote.sendRequest(COMMAND_SET_MAX_NETWORK_THREAD_COUNT, data, reply, option);
            reply.readException();
            boolean result = reply.readInt() == 1 ? true : false;
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public long getSofar(
            /* [in] */ int downloadId) throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(downloadId);

        try {
            remote.sendRequest(COMMAND_GET_SOFAR, data, reply, option);
            reply.readException();
            long result = reply.readLong();
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public long getTotal(
            /* [in] */ int downloadId) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(downloadId);

        try {
            remote.sendRequest(COMMAND_GET_TOTAL, data, reply, option);
            reply.readException();
            long result = reply.readLong();
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public byte getStatus(
            /* [in] */ int downloadId) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(downloadId);

        try {
            remote.sendRequest(COMMAND_GET_STATUS, data, reply, option);
            reply.readException();
            byte result = (byte) reply.readInt();
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public boolean isIdle() throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        try {
            remote.sendRequest(COMMAND_IS_IDLE, data, reply, option);
            reply.readException();
            boolean result = reply.readInt() == 1 ? true : false;
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void startForeground(
            /* [in] */ int id,
            /* [in] */ NotificationRequest notification) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(id);
        data.writeSequenceable(notification);

        try {
            remote.sendRequest(COMMAND_START_FOREGROUND, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void stopForeground(
            /* [in] */ boolean removeNotification) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(removeNotification ? 1 : 0);

        try {
            remote.sendRequest(COMMAND_STOP_FOREGROUND, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public boolean clearTaskData(
            /* [in] */ int id) throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        data.writeInt(id);

        try {
            remote.sendRequest(COMMAND_CLEAR_TASK_DATA, data, reply, option);
            reply.readException();
            boolean result = reply.readInt() == 1 ? true : false;
            return result;
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }

    @Override
    public void clearAllTaskData() throws RemoteException {

        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);

        data.writeInterfaceToken(DESCRIPTOR);
        try {
            remote.sendRequest(COMMAND_CLEAR_ALL_TASK_DATA, data, reply, option);
            reply.readException();
        } finally {
            data.reclaim();
            reply.reclaim();
        }
    }
};

